package city.spring.modules.health.service.impl;

import city.spring.modules.health.entity.PhysicalExaminationEntity;
import city.spring.modules.health.repository.PhysicalExaminationRepository;
import city.spring.modules.health.service.PhysicalExaminationService;
import city.spring.modules.health.vo.PhysicalExaminationVO;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * Service：体检记录
 *
 * @author 谢金婷
 * @date 2020-08-04 14:15:52
 */
@CacheConfig(cacheNames = {PhysicalExaminationServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class PhysicalExaminationServiceImpl extends ServiceImpl<PhysicalExaminationRepository, PhysicalExaminationEntity> implements PhysicalExaminationService {

    private final UserService userService;

    private final PhysicalExaminationRepository physicalExaminationRepository;

    public PhysicalExaminationServiceImpl(UserService userService, PhysicalExaminationRepository physicalExaminationRepository) {
        this.userService = userService;
        this.physicalExaminationRepository = physicalExaminationRepository;
    }

    @Override
    public void savePhysicalExamination(PhysicalExaminationEntity entity, @NonNull Principal user) {
        //设置登记人员为当前登录用户
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setRegisterperson(userEntity.getId());

        entity.setChecktime(new Date());

        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updatePhysicalExamination(PhysicalExaminationEntity entity, @NonNull Principal user) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改

        //设置登记人员为当前登录用户
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setRegisterperson(userEntity.getId());

        entity.setChecktime(new Date());

        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deletePhysicalExamination(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deletePhysicalExamination(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public PhysicalExaminationEntity getById(String id) {
        return physicalExaminationRepository.getById(id);
    }

    @Override
    public IPage<PhysicalExaminationVO> listPage(Page<? extends PhysicalExaminationEntity> page, Wrapper<? extends PhysicalExaminationEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}